Expand description
This is a base16 (e.g. hexadecimal) encoding and decoding library with an emphasis on performance. The API is very similar and inspired by the base64 crate’s API, however it’s less complex (base16 is much more simple than base64).
§Encoding
The config options at the moment are limited to the output case (upper vs lower).
Function | Output | Allocates | Requires alloc feature |
---|---|---|---|
encode_upper , encode_lower | Returns a new String | Always | Yes |
encode_config | Returns a new String | Always | Yes |
encode_config_buf | Appends to provided String | If buffer needs to grow | Yes |
encode_config_slice | Writes to provided &[u8] | Never | No |
§Decoding
Note that there are no config options (In the future one might be added to restrict the input character set, but it’s not clear to me that this is useful).
Function | Output | Allocates | Requires alloc feature |
---|---|---|---|
decode | Returns a new Vec<u8> | Always | Yes |
decode_slice | Writes to provided &[u8] | Never | No |
decode_buf | Appends to provided Vec<u8> | If buffer needs to grow | Yes |
§Features
This crate has two features, both are enabled by default and exist to allow
users in no_std
environments to disable various portions of .
-
The
"alloc"
feature, which is on by default, adds a number of helpful functions that require use of thealloc
crate, but not the rest ofstd
.- This is
no_std
compatible. - Each function should list whether or not it requires this feature
under the
Availability
of its documentation.
- This is
-
The
"std"
feature, which is on by default, enables the"alloc"
feature, and additionally makesDecodeError
implement thestd::error::Error
trait.- Frustratingly, this trait is in
std
(and not incore
oralloc
), but not implementing it would be quite annoying for some users, so it’s kept, even though it’s what prevents us from beingno_std
compatible in all configurations.
- Frustratingly, this trait is in
Re-exports§
pub use EncConfig::*;
Enums§
- Represents a problem with the data we want to decode.
- Configuration options for encoding. Just specifies whether or not output should be uppercase or lowercase.
Functions§
- Decode bytes from base16, and return a new
Vec<u8>
containing the results. - Decode bytes from base16, and appends into the provided buffer. Only allocates if the buffer could not fit the data. Returns the number of bytes written.
- Decode a single character as hex.
- Decode bytes from base16, and write into the provided buffer. Never allocates.
- Encode a single character as hex, returning a tuple containing the two encoded bytes in big-endian order – the order the characters would be in when written out (e.g. the top nibble is the first item in the tuple)
- Convenience wrapper for
base16::encode_byte(byte, base16::EncodeLower)
- Convenience wrapper for
base16::encode_byte(byte, base16::EncodeUpper)
- Encode
input
into a string using the listed config. The resulting string containsinput.len() * 2
bytes. - Encode
input
into the end of the provided buffer. Returns the number of bytes that were written. - Write bytes as base16 into the provided output buffer. Never allocates.
- Encode bytes as base16, using lower case characters for nibbles between 10 and 15 (
a
throughf
). - Encode bytes as base16, using upper case characters for nibbles between 10 and 15 (
A
throughF
).